package domain.entities;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 22/08/12
 * Time: 15:36
 * To change this template use File | Settings | File Templates.
 */
public class DoctorDegreeDAO extends HibernateDaoSupport {
    private static final Logger log = LoggerFactory
            .getLogger(DoctorDegreeDAO.class);
    // property constants
    public static final String ID = "id";
    public static final String DESCRIPCION = "descripcion";

    protected void initDao() {
        // do nothing
    }

    public void save(DoctorDegree transientInstance) {
        log.debug("saving DoctorDegree instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(DoctorDegree persistentInstance) {
        log.debug("deleting DoctorDegree instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public DoctorDegree findById(Integer id) {
        log.debug("getting DoctorDegree instance with id: " + id);
        try {
            DoctorDegree instance = (DoctorDegree) getHibernateTemplate()
                    .get("domain.entities.DoctorDegree", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public DoctorDegree findByDescripcion(String descripcion) {
        try {
            String queryString = "from DoctorDegree where descripcion like '" + descripcion + "'" ;
            return (DoctorDegree)getHibernateTemplate().find(queryString).get(0);
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<DoctorDegree> findByExample(DoctorDegree instance) {
        log.debug("finding DoctorDegree instance by example");
        try {
            List<DoctorDegree> results = (List<DoctorDegree>) getHibernateTemplate()
                    .findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding DoctorDegree instance with property: "
                + propertyName + ", value: " + value);
        try {
            String queryString = "from DoctorDegree as model where model."
                    + propertyName + "= ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<DoctorDegree> findById(Object id) {
        return findByProperty(ID, id);
    }

    public List<DoctorDegree> findByDescripcion(Object name) {
        return findByProperty(DESCRIPCION, name);
    }

    public boolean existByDescripcion(String descripcion) {
        try {
            String queryString = "from DoctorDegree where descripcion like '" + descripcion + "'" ;
            return (getHibernateTemplate().find(queryString).size() > 0);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findAll() {
        log.debug("finding all DoctorDegree instances");
        try {
            String queryString = "from DoctorDegree";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public DoctorDegree merge(DoctorDegree detachedInstance) {
        log.debug("merging DoctorDegree instance");
        try {
            DoctorDegree result = (DoctorDegree) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(DoctorDegree instance) {
        log.debug("attaching dirty DoctorDegree instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(DoctorDegree instance) {
        log.debug("attaching clean DoctorDegree instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static DoctorDegreeDAO getFromApplicationContext(
            ApplicationContext ctx) {
        return (DoctorDegreeDAO) ctx.getBean("DoctorDegreeDAO");
    }
}
